package cn.cheln.explorer.ds.database;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore.Audio.Media;
import android.widget.Toast;
import cn.cheln.explorer.config.Config;
import cn.cheln.explorer.ds.MediaArgs;
import cn.cheln.support.data.db.CursorUtil;
import cn.cheln.support.data.text.xml.SPUtil;
import cn.cheln.support.manager.ActivityPool;
import com.direction.hold.ui.CountActivity;
import java.io.File;

public class SQLManager
{
  public static String SEARCH_FIELD;
  public static Uri SelfUri;
  public static String Table_image;
  public static String Table_music;
  public static String Table_video;
  public static String createFileSortIdxSql;
  public static File dbDir;
  public static File dbFile;
  public static String dbfileName = "files.db";
  public static final String dropFileIdxSql = "DROP INDEX path_index";
  public static final String dropFileTitleIdxSql = "DROP INDEX title_index";
  public static String otherfile;
  public static final String packageName = "com.lewa.app.filemanager";
  public static String querySql;
  public static SQLiteDatabase sdb;
  public static String tabledir;
  public static String tableimage;
  public static String tablename;
  public static final String unknown = "WEI ZHI";

  static
  {
    SEARCH_FIELD = "distinct _data,sort_key";
    Table_image = "image";
    Table_music = "files";
    Table_video = "files";
    tablename = "files";
    tableimage = "image";
    tabledir = "dir";
    querySql = "distinct m._data _data,m.title title, m.artist_id artist_id,m.album_id album_id,m._size _size,m.date_modified date_modified,_display_name,g.name genre,case when b.album like '<unknown>' then 'unknown'  else b.album end album,case when i.artist is null then 'unknown' when i.artist like '<unknown>' then 'unknown'  else i.artist end artist,t._data thumbnail from audio_meta m left join (select p.audio_id _id,s.name name from audio_genres_map p left join audio_genres s on (s._id = p.genre_id)) g on (m._id = g._id) left join album_art t on (m.album_id=t.album_id ) left join artists i on (m.artist_id=i.artist_id) left join albums b on (m.album_id = b.album_id)";
    createFileSortIdxSql = "CREATE INDEX IF NOT EXISTS sort_key_index ON " + tablename + "(sort_key)";
    initFileOfDB();
    SelfUri = Uri.parse("content://com.lewa.app.filemanager/files");
  }

  public static boolean apkFlagExists(Context paramContext)
  {
    return ifFieldExists("files", "apk_condition_install", paramContext).booleanValue();
  }

  public static boolean createFileDB(Context paramContext)
  {
    label68: Cursor localCursor1;
    boolean bool = true;
    Object localObject = SPUtil.getValue(paramContext, "File", "dbName", String.class);
    if (localObject != null)
    {
      dbfileName = localObject.toString();
      bool = initFileOfDB();
    }
    if (Config.is4_0Lower)
      new File("/data/data/com.lewa.app.filemanager/databases/files.db").delete();
    if (!(dbDir.exists()))
      dbDir.mkdirs();
    int i = 1;
    int j = 0;
    if (ifCreateDBSuccessful(bool))
    {
      if (j != 0)
        SPUtil.putValue(paramContext, "File", "dbName", dbfileName);
      localCursor1 = queryTableExists(tablename);
      if (!(bool))
        break label407;
      if ((localCursor1 == null) || (localCursor1.getCount() == 0))
      {
        sdb.execSQL(createFileTable(tablename));
        sdb.execSQL(createIndexOfTableFile(tablename));
      }
    }
    while (true)
    {
      Cursor localCursor2 = queryTableExists(tableimage);
      if ((localCursor2 == null) || (localCursor2.getCount() == 0))
      {
        sdb.execSQL(createFileTable(tableimage));
        sdb.execSQL(createIndexOfTableFile(tableimage));
        sdb.execSQL("delete from files where mime_type like 'image%'");
      }
      Cursor localCursor3 = queryTableExists(tabledir);
      if ((localCursor3 == null) || (localCursor3.getCount() == 0))
      {
        sdb.execSQL(createFileTable(tabledir));
        sdb.execSQL(createIndexOfTableFile(tabledir));
        sdb.execSQL("delete from files where format = 12289");
      }
      try
      {
        if (!(ifFieldExists("files", "format", paramContext).booleanValue()))
          sdb.execSQL("alter table files add format INTEGER");
        if (!(ifFieldExists("files", "sort_key", paramContext).booleanValue()))
          sdb.execSQL("alter table files add sort_key TEXT");
        if (!(ifIdxExists("sort_key_index", paramContext).booleanValue()))
          sdb.execSQL(createIndexOfTableFile(tablename));
        label320: CursorUtil.closeCursor(localCursor3);
        CursorUtil.closeCursor(localCursor2);
        CursorUtil.closeCursor(localCursor1);
        sdb.close();
        return false;
        dbFile.delete();
        dbfileName += i;
        if (++i > 3)
        {
          Toast.makeText(CountActivity.categoryActivity, 2131034312, 1).show();
          ActivityPool.getInstance().exit();
          return false;
        }
        j = 1;
        break label68:
        if (ifIdxExists("path_index", paramContext).booleanValue())
          label407: sdb.execSQL("DROP INDEX path_index");
        if (ifIdxExists("title_index", paramContext).booleanValue());
        sdb.execSQL("DROP INDEX title_index");
      }
      catch (Exception localException)
      {
        localException.printStackTrace();
        break label320:
      }
    }
  }

  public static String createFileTable(String paramString)
  {
    return "CREATE TABLE IF NOT EXISTS " + paramString + " (_id INTEGER PRIMARY KEY AUTOINCREMENT,_data TEXT,_size INTEGER,mime_type TEXT,date_modified INTEGER,title TEXT,sort_key TEXT,format INTEGER)";
  }

  public static String createIndexOfTableFile(String paramString)
  {
    return "CREATE INDEX IF NOT EXISTS sort_key_index ON " + paramString + "(sort_key)";
  }

  private static String getMatchCondition(String paramString, boolean paramBoolean)
  {
    String str1 = "(" + getSortKey() + " like '%*" + paramString + "%'" + " or " + getSortKey() + " like '%|%" + paramString + "%'" + ")";
    String str2 = "(" + getName() + " like '%" + paramString + "%'" + ")";
    return "(" + str2 + " or " + str1 + ")";
  }

  private static String getName()
  {
    return " title ";
  }

  public static String getSearchPart_Apk_Sql(String paramString1, String paramString2, String[] paramArrayOfString)
  {
    return paramArrayOfString[0] + ", '" + ShowOrder.apkOrder + "' flagorder from files where " + getMatchCondition(paramString1, true) + " and mime_type like '" + "application/vnd.shortcut.package-archive" + "'" + paramString2;
  }

  public static String getSearchPart_Files_Sql(String paramString1, String paramString2, String[] paramArrayOfString)
  {
    return paramArrayOfString[0] + ", '" + ShowOrder.docOrder + "' flagorder from files where (mime_type like 'text/%' or (mime_type like 'application/%' and mime_type not like 'application/vnd.shortcut.package-archive')) and " + getMatchCondition(paramString1, false) + paramString2;
  }

  public static String getSearchPart_Folder_Sql(String paramString1, String paramString2, String[] paramArrayOfString)
  {
    return paramArrayOfString[0] + ", '" + ShowOrder.folderOrder + "' flagorder from dir where " + getMatchCondition(paramString1, false) + " " + paramString2;
  }

  public static String getSearchPart_Image_Sql(String paramString1, String paramString2, Float paramFloat, String[] paramArrayOfString)
  {
    return paramArrayOfString[0] + ", '" + ShowOrder.picOrder + "' flagorder from " + Table_image + " where " + getMatchCondition(paramString1, false) + " and _size > " + (1024.0F * paramFloat.floatValue()) + paramString2;
  }

  public static String getSearchPart_Music_Sql(String paramString1, String paramString2, String[] paramArrayOfString)
  {
    return paramArrayOfString[0] + ", '" + ShowOrder.musicOrder + "' flagorder from " + Table_music + " where " + getMatchCondition(paramString1, true) + " and substr(mime_type,0,6) = 'audio'" + paramString2;
  }

  public static String getSearchPart_Video_Sql(String paramString1, String paramString2, String[] paramArrayOfString)
  {
    return paramArrayOfString[0] + ", '" + ShowOrder.videoOrder + "' flagorder from " + Table_video + " where " + getMatchCondition(paramString1, true) + " and mime_type like 'video/%'" + paramString2;
  }

  private static String getSortKey()
  {
    return "sort_key";
  }

  private static boolean ifCreateDBSuccessful(boolean paramBoolean)
  {
    if (paramBoolean);
    try
    {
      Object localObject = SQLiteDatabase.openOrCreateDatabase(otherfile, null);
      sdb = (SQLiteDatabase)localObject;
      return true;
      SQLiteDatabase localSQLiteDatabase = SQLiteDatabase.openDatabase(otherfile, null, -1);
      localObject = localSQLiteDatabase;
    }
    catch (Exception localException)
    {
    }
    return false;
  }

  public static Boolean ifFieldExists(String paramString1, String paramString2, Context paramContext)
  {
    ContentResolver localContentResolver = paramContext.getContentResolver();
    Uri localUri = MediaArgs.otherUri;
    String[] arrayOfString = new String[1];
    arrayOfString[0] = " sql from sqlite_master where tbl_name='" + paramString1 + "'--";
    Cursor localCursor = localContentResolver.query(localUri, arrayOfString, null, null, null);
    boolean bool = false;
    if (localCursor != null)
    {
      int i = localCursor.getCount();
      bool = false;
      if (i > 0)
      {
        localCursor.moveToNext();
        bool = localCursor.getString(0).contains(paramString2);
      }
    }
    CursorUtil.closeCursor(localCursor);
    return Boolean.valueOf(bool);
  }

  public static Boolean ifIdxExists(String paramString, Context paramContext)
  {
    boolean bool = true;
    ContentResolver localContentResolver = paramContext.getContentResolver();
    Uri localUri = MediaArgs.otherUri;
    String[] arrayOfString = new String[bool];
    arrayOfString[0] = " 1 from sqlite_master where type='index' and name='" + paramString + "'--";
    Cursor localCursor = localContentResolver.query(localUri, arrayOfString, null, null, null);
    if ((localCursor != null) && (localCursor.getCount() > 0));
    while (true)
    {
      CursorUtil.closeCursor(localCursor);
      return Boolean.valueOf(bool);
      bool = false;
    }
  }

  private static boolean initFileOfDB()
  {
    otherfile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/LEWA/File" + "/databases/" + dbfileName;
    dbFile = new File(otherfile);
    dbDir = new File(dbFile.getParent());
    while (true)
      try
      {
        if (!(dbFile.exists()))
        {
          dbFile.createNewFile();
          return true;
        }
        return false;
      }
      catch (Exception localException)
      {
        localException.printStackTrace();
      }
  }

  public static Cursor queryMusic(Context paramContext, String paramString)
  {
    ContentResolver localContentResolver = paramContext.getContentResolver();
    Uri localUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    String[] arrayOfString = new String[1];
    arrayOfString[0] = querySql + " order by " + paramString + "--";
    return localContentResolver.query(localUri, arrayOfString, null, null, null);
  }

  private static Cursor queryTableExists(String paramString)
  {
    return sdb.query("sqlite_master", new String[] { "sql" }, "type=? and name =?", new String[] { "table", paramString }, null, null, null);
  }

  public static final class ShowOrder
  {
    public static int apkOrder;
    public static int docOrder;
    public static int folderOrder;
    public static int musicOrder;
    public static int picOrder = 0;
    public static int videoOrder;

    static
    {
      musicOrder = 1;
      videoOrder = 2;
      docOrder = 3;
      apkOrder = 4;
      folderOrder = -1;
    }
  }
}

/* Location:           H:\360云盘\lewa_explorer_3.0\bin\FileApp_orig_dex2jar.jar
 * Qualified Name:     cn.cheln.explorer.ds.database.SQLManager
 * JD-Core Version:    0.5.2
 */